package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.bitmap.BitmapDisplayConfig;
import com.lidroid.xutils.bitmap.callback.BitmapLoadCallBack;
import com.lidroid.xutils.bitmap.callback.BitmapLoadFrom;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.WatchUser;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by E540 on 2015/3/13.
 */
public class WatchUserAdapter extends RecyclerView.Adapter<WatchUserAdapter.WatchUserViewHolder> {
    private Context context;
    private List<WatchUser> data;

    public WatchUserAdapter(Context context, List<WatchUser> data) {
        this.context = context;
        this.data = data;
    }



    @Override
    public void onBindViewHolder(WatchUserViewHolder holder, int position) {
        BitmapUtils utils = DataStore.getBitmapUtils();
        WatchUser watchUser = data.get(position);
        utils.configDefaultLoadingImage(R.drawable.icon_default);
        utils.display(holder.pic,watchUser.getHeadPic(),new BitmapLoadCallBack<ImageView>(){

            @Override
            public void onLoadCompleted(ImageView container, String uri, Bitmap bitmap, BitmapDisplayConfig config, BitmapLoadFrom from) {
                int width = bitmap.getWidth();
                Bitmap dst = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
                Paint paint = new Paint();
                paint.setShader(new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP));
                Canvas canvas = new Canvas(dst);
                canvas.drawCircle(width / 2, width / 2, width / 2, paint);
                container.setImageBitmap(dst);
            }

            @Override
            public void onLoadFailed(ImageView container, String uri, Drawable drawable) {
                container.setImageDrawable(drawable);
            }
        });
    }

    @Override
    public WatchUserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.item_animation_detial_watch_user, parent, false);

        return new WatchUserViewHolder(view);
    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        } else {
            return 0;
        }
    }

    public static class WatchUserViewHolder extends RecyclerView.ViewHolder {
        @ViewInject(R.id.item_animation_detail_watch_user_pic)
        public ImageView pic;
        public WatchUserViewHolder(View itemView) {
            super(itemView);
            ViewUtils.inject(this, itemView);
        }
    }
}
